package com.jsmtr.rms.controller;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jsmtr.rms.entity.*;
import nc.webservice.zp.BusinessExceptionException;
import nc.webservice.zp.ICrtZPServiceStub;
import nc.webservice.zp.ICrtZPServiceStub.GetPersonInfo;

import org.apache.axis2.AxisFault;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jsmtr.rms.entity.vo.JobList;
import com.jsmtr.rms.entity.vo.NZUserWorkForm;
import com.jsmtr.rms.service.INZService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.BeanTools;
import com.jsmtr.rms.utils.DateTools;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.PhoneTools;
import com.jsmtr.rms.utils.RightInterceptor;
import com.jsmtr.rms.utils.StringTools;


@Controller
@RequestMapping("/NZ")
public class NZController {

    @Resource
    private IUtilService utilService;
    @Resource
    private INZService nzService;

    //[start] HR接口

    public static void main(String[] args) {
        XMLtoMap(getinfo("04344"));
    }

    //读取人员信息
    private static String getinfo(String code) {
        try {
            GetPersonInfo xxx = new GetPersonInfo();
            xxx.setString(code);
            ICrtZPServiceStub stub = new ICrtZPServiceStub();
            try {
                String xml = stub.getPersonInfo(xxx).get_return();
                System.out.println(xml);
                return xml;
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (BusinessExceptionException e) {
                e.printStackTrace();
            }
            return null;
        } catch (AxisFault e) {
            e.printStackTrace();
            return null;
        }

    }

    //从XML中获得手机号
    @SuppressWarnings({"rawtypes"})
    private static String getTel(String xml) {
        try {
            Document doc = (Document) DocumentHelper.parseText(xml);
            Element books = doc.getRootElement();
            Iterator Elements = books.elementIterator();
            while (Elements.hasNext()) {
                Element user = (Element) Elements.next();
                if (user.attribute("groupname").getText().equals("psnsql")) {
                    List nodes = ((Element) user.elements().get(0)).elements();
                    for (Object obj : nodes) {
                        Element eobj = (Element) obj;
                        if (eobj.attribute("fieldname").getText().equals("mobile")) {
                            return eobj.getTextTrim();
                        }
                    }
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //解析XML
    @SuppressWarnings({"unchecked"})
    public static Map<String, Object> XMLtoMap(String xml) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            Document doc = (Document) DocumentHelper.parseText(xml);
            Element root = doc.getRootElement();
            List<Element> group = root.elements("group");

            List<Object> edu = new ArrayList<Object>();
            List<Object> work = new ArrayList<Object>();
            Map<String, Object> jbxx = new HashMap<String, Object>();

            for (Element element : group) {
                //教育背景
                if (element.attributeValue("groupname").equals("edusql")) {
                    List<Element> edus = element.elements("row");
                    for (Element element2 : edus) {
                        Map<String, Object> edumap = new HashMap<String, Object>();
                        List<Element> eduobj = element2.elements("field");
                        for (Element element3 : eduobj) {
                            edumap.put(element3.attributeValue("fieldname"), element3.getTextTrim());
                        }
                        edu.add(edumap);
                    }
                }
                //工作履历
                if (element.attributeValue("groupname").equals("worksql")) {
                    List<Element> works = element.elements("row");
                    for (Element element2 : works) {
                        Map<String, Object> workmap = new HashMap<String, Object>();
                        List<Element> eduobj = element2.elements("field");
                        for (Element element3 : eduobj) {
                            workmap.put(element3.attributeValue("fieldname"), element3.getTextTrim());
                        }
                        work.add(workmap);
                    }
                }
                //基本信息
                if (element.attributeValue("groupname").equals("psnsql")) {
                    List<Element> xxele = ((Element) element.elements("row").get(0)).elements("field");
                    for (Element element2 : xxele) {
                        jbxx.put(element2.attributeValue("fieldname"), element2.getTextTrim());
                    }
                }

            }

            map.put("jbxx", jbxx);
            map.put("work", work);
            map.put("edu", edu);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


    //[end]

    //[start] 员工登录
    @RequestMapping("/login")
    public String Login() {
        return "NZ/login";
    }

    //登录
    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequestMapping(value = "/LoginAction", method = RequestMethod.POST)
    public void LoginAction(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        try {
            String ucode = request.getParameter("ucode").trim();
            String vcode = request.getParameter("vcode").trim();
            if (session.getAttribute(RightInterceptor.SEESION_NZVCODE).equals(ucode + "-" + vcode)) {
                NZResume resume = nzService.getResume(ucode);
                Map<String, Object> map = XMLtoMap(getinfo(ucode));
                if (resume == null) {
                    List addList = new ArrayList();
                    //基本信息
                    Map<String, Object> jbxx = (Map<String, Object>) map.get("jbxx");
                    resume = new NZResume();
                    resume.setAge(Integer.parseInt(jbxx.get("age").toString()));
                    resume.setBirthday(jbxx.get("birthdate").toString());
                    resume.setCode(jbxx.get("psncode").toString());
                    resume.setDept1(jbxx.get("deptdoc_ss").toString());
                    resume.setDept2(jbxx.get("deptdoc_sz").toString());
                    resume.setGroupdate(jbxx.get("joinsysdate").toString());
                    resume.setId(UUID.randomUUID().toString());
                    resume.setJndj(jbxx.get("jndj").toString());
                    resume.setJob(jbxx.get("pk_om_job").toString());
                    resume.setName(jbxx.get("psnname").toString());
                    resume.setPosition(jbxx.get("zyjszw").toString());
                    resume.setRsdate(jbxx.get("indutydate").toString());
                    resume.setSex(jbxx.get("sex").toString());
                    resume.setTel(jbxx.get("mobile").toString());
                    resume.setZzmm(jbxx.get("polity").toString());

                    //学历信息
                    List<Object> edus = (List<Object>) map.get("edu");
                    for (Object _edu : edus) {
                        Map<String, Object> edu = (Map<String, Object>) _edu;
                        NZResumeEducation education = new NZResumeEducation();
                        education.setCode(jbxx.get("psncode").toString());
                        education.setEducation(edu.get("education").toString());
                        education.setId(UUID.randomUUID().toString());
                        education.setMajor(edu.get("groupdef3").toString());
                        String top = edu.get("lasteducation").toString().trim();
                        if (top.equals("Y")) {
                            education.setTop(1);
                            resume.setEducation(edu.get("education").toString());//最高学历
                        } else {
                            education.setTop(0);
                        }
                        education.setXxmc(edu.get("school").toString());

                        addList.add(education);
                    }
                    //工作履历
                    List<Object> works = (List<Object>) map.get("work");
                    for (Object _work : works) {
                        Map<String, Object> work = (Map<String, Object>) _work;
                        NZResumeWork workinfo = new NZResumeWork();
                        workinfo.setCode(jbxx.get("psncode").toString());
                        workinfo.setCompany(work.get("workcorp").toString());
                        workinfo.setDept(work.get("workdept").toString());
                        workinfo.setEnddate(work.get("enddate").toString());
                        workinfo.setId(UUID.randomUUID().toString());
                        workinfo.setJob(work.get("workpost").toString());
                        workinfo.setStartdate(work.get("begindate").toString());
                        workinfo.setHrid(work.get("pk_psndoc_sub").toString());
                        workinfo.setWorkcont(work.get("groupdef1").toString());
                        workinfo.setWorkzz(work.get("vworkduty").toString());

                        addList.add(workinfo);
                    }

                    addList.add(resume);

                    utilService.addList(addList);
                } else {
                    List addList = new ArrayList();
                    List delList = new ArrayList();
                    //基本信息
                    Map<String, Object> jbxx = (Map<String, Object>) map.get("jbxx");
                    resume.setAge(Integer.parseInt(jbxx.get("age").toString()));
                    resume.setBirthday(jbxx.get("birthdate").toString());
                    resume.setCode(jbxx.get("psncode").toString());
                    resume.setDept1(jbxx.get("deptdoc_ss").toString());
                    resume.setDept2(jbxx.get("deptdoc_sz").toString());
                    resume.setGroupdate(jbxx.get("joinsysdate").toString());
                    resume.setJndj(jbxx.get("jndj").toString());
                    resume.setJob(jbxx.get("pk_om_job").toString());
                    resume.setName(jbxx.get("psnname").toString());
                    resume.setPosition(jbxx.get("zyjszw").toString());
                    resume.setRsdate(jbxx.get("indutydate").toString());
                    resume.setSex(jbxx.get("sex").toString());
                    resume.setTel(jbxx.get("mobile").toString());
                    resume.setZzmm(jbxx.get("polity").toString());

                    //学历信息
                    List<Object> edus = (List<Object>) map.get("edu");
                    for (Object _edu : edus) {
                        Map<String, Object> edu = (Map<String, Object>) _edu;
                        NZResumeEducation education = new NZResumeEducation();
                        education.setCode(jbxx.get("psncode").toString());
                        education.setEducation(edu.get("education").toString());
                        education.setId(UUID.randomUUID().toString());
                        education.setMajor(edu.get("groupdef3").toString());
                        String top = edu.get("lasteducation").toString().trim();
                        if (top.equals("Y")) {
                            education.setTop(1);
                            resume.setEducation(edu.get("education").toString());//最高学历
                        } else {
                            education.setTop(0);
                        }
                        education.setXxmc(edu.get("school").toString());

                        addList.add(education);
                    }
                    delList.addAll(nzService.getEducations(ucode));

                    List<NZResumeWork> oldworks = nzService.getWorks(ucode);
                    delList.addAll(oldworks);

                    //工作履历
                    List<Object> works = (List<Object>) map.get("work");
                    for (Object _work : works) {
                        Map<String, Object> work = (Map<String, Object>) _work;

                        NZResumeWork workinfo = new NZResumeWork();

                        String hrid = work.get("pk_psndoc_sub").toString();
                        for (NZResumeWork object : oldworks) {
                            if (object.getHrid().equals(hrid)) {
                                workinfo.setWorkremark(object.getWorkremark());
                            }
                        }

                        workinfo.setCode(jbxx.get("psncode").toString());
                        workinfo.setCompany(work.get("workcorp").toString());
                        workinfo.setDept(work.get("workdept").toString());
                        workinfo.setEnddate(work.get("enddate").toString());
                        workinfo.setId(UUID.randomUUID().toString());
                        workinfo.setJob(work.get("workpost").toString());
                        workinfo.setStartdate(work.get("begindate").toString());
                        workinfo.setHrid(work.get("pk_psndoc_sub").toString());
                        workinfo.setWorkcont(work.get("groupdef1").toString());
                        workinfo.setWorkzz(work.get("vworkduty").toString());

                        addList.add(workinfo);
                    }
                    utilService.editList(resume, delList, addList);
                }
                session.setAttribute(RightInterceptor.SEESION_NZUSER, resume);
                utilService.printJsonResult(true, "登录成功", "/NZ/", response);
            } else {
                utilService.printJsonResult(false, "验证码无效", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "登录失败", "no", response);
        }
    }

    //发送手机验证码
    @RequestMapping(value = "/SendVCode", method = RequestMethod.POST)
    public void SendVCode(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        try {
            String ucode = request.getParameter("ucode").trim();
            String tel = getTel(getinfo(ucode));
            if (tel != null) {
                BaseMessageConfig messageConfig=utilService.getMessageConfig();
                String vcode = StringTools.getRandNum(4);
                if (PhoneTools.sendMsg(tel, "招聘系统登录验证码：" + vcode,messageConfig.getUsername(),messageConfig.getPassword(),messageConfig.getHost()) == true) {
                    session.setAttribute(RightInterceptor.SEESION_NZVCODE, ucode + "-" + vcode);
                    utilService.printJsonResult(true, "验证码已发送到你尾号为" + tel.substring(7) + "的手机", "no", response);
                } else {
                    utilService.printJsonResult(false, "验证码发送失败，请联系管理员", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "未找到你的手机号，请联系管理员", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "验证码发送失败，请联系管理员", "no", response);
        }
    }

    //获得登录信息
    @RequestMapping("/getuserinfo")
    public void getNZUserInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        try {
            NZResume resume = (NZResume) session.getAttribute(RightInterceptor.SEESION_NZUSER);
            StringBuilder str = new StringBuilder();
            if (resume != null) {
                str.append("<div class='user'>你好：" + resume.getName() + "<a href='javascript:logout();'>[注销]</a></div>");
                str.append("<a href='/NZ/resume'>我的简历</a><a href='/NZ/myjob'>我的应聘</a>");
            } else {
                str.append("<a href='/NZ/login'>立即登录</a>");
            }
            utilService.printTextResult(str.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("", response);
        }
    }

    //退出登录
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public void logout(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        try {
            session.removeAttribute(RightInterceptor.SEESION_NZUSER);
            utilService.printJsonResult(true, "注销成功！", "/NZ/", response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "注销失败！", "no", response);
        }
    }


    //[end]

    //[start] 我的简历
    @RequestMapping("/resume")
    public String Resume(HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            String code = utilService.getNZUser(request).getCode();
            //基本信息
            NZResume resume = nzService.getResume(code);
            StringBuilder str = new StringBuilder();
            str.append("<tr><td width='20' rowspan='4' class='title'><b>基本信息</b></td>");
            str.append("<td width='70'>员工号:</td><td width='170'>" + resume.getCode() + "</td>");
            str.append("<td width='70'>姓名:</td><td width='170'>" + resume.getName() + "</td>");
            str.append("<td width='110'>性别:</td><td>" + resume.getSex() + "</td></tr>");
            str.append("<tr><td>出生日期:</td><td>" + resume.getBirthday() + "</td>");
            str.append("<td>政治面貌:</td><td>" + resume.getZzmm() + "</td>");
            str.append("<td>进入集团日期:</td><td>" + resume.getGroupdate() + "</td></tr>");
            str.append("<tr><td>工作部门:</td><td>" + resume.getDept1() + "</td>");
            str.append("<td>工作岗位:</td><td>" + resume.getJob() + "</td>");
            str.append("<td>专业技术职务:</td><td>" + resume.getPosition() + "</td></tr>");
            str.append("<tr><td>技能等级:</td><td>" + (resume.getJndj().equals("null") ? "" : resume.getJndj()) + "</td>");
            str.append("<td>联系电话:</td><td colspan='3'>" + resume.getTel() + "</td></tr>");
            model.addAttribute("resume", str);

            //教育背景
            StringBuilder str1 = new StringBuilder();
            int ecount = 1;
            List<NZResumeEducation> educations = nzService.getEducations(code);
            for (NZResumeEducation education : educations) {
                str1.append("<tr><td>" + education.getXxmc() + "</td>");
                str1.append("<td>" + education.getMajor() + "</td>");
                str1.append("<td>" + education.getEducation() + "</td></tr>");
                ecount++;
            }
            model.addAttribute("education", str1);
            model.addAttribute("ecount", ecount);

            //工作履历
            StringBuilder str2 = new StringBuilder();
            List<NZResumeWork> works = nzService.getWorks(code);
            int i = 0;
            for (NZResumeWork work : works) {
                str2.append("<input type='hidden' name='work[" + i + "].id' value='" + work.getId() + "'/>");
                str2.append("<table class='treeTable' style='margin-top:5px;'><tbody>");
                str2.append("<tr><td width='20' rowspan='13' class='title'><b>工作履历</b></td>");
                str2.append("<td>工作单位:</td><td colspan='5'>" + work.getCompany() + "</td></tr>");
                str2.append("<tr><td width='70'>工作部门:</td><td width='135'>" + work.getDept() + "</td>");
                str2.append("<td width='70'>工作岗位:</td><td width='180'>" + (work.getJob().equals("null") ? "" : work.getJob()) + "</td>");
                str2.append("<td width='70'>起止日期:</td><td>" + work.getStartdate() + " - " + (work.getEnddate().equals("null") ? "" : work.getEnddate()) + "</td></tr>");
                str2.append("<tr><td>工作内容:</td><td colspan='5'>" + (work.getWorkcont().equals("null") ? "" : work.getWorkcont()) + "</td></tr>");
                str2.append("<tr><td>工作职责:</td><td colspan='5'>" + (work.getWorkzz().equals("null") ? "" : work.getWorkzz()) + "</td></tr>");
                str2.append("<tr><td>工作经历<br>工作经验:</td><td colspan='5'>");
                str2.append("<textarea name='work[" + i + "].workremark' datatype='*' nullmsg='工作经历经验不能为空！'>" + (work.getWorkremark() == null ? "" : work.getWorkremark()) + "</textarea></td>");
                str2.append("</tr></tbody></table>");
                i++;
            }
            model.addAttribute("work", str2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "NZ/resume";
    }

    //保存简历
    @RequestMapping(value = "/ResumeAction", method = RequestMethod.POST)
    public void ResumeAction(HttpServletRequest request, HttpServletResponse response, NZUserWorkForm work) {
        try {
            List<NZResumeWork> editList = new ArrayList<NZResumeWork>();
            List<NZResumeWork> works = work.getWork();
            for (NZResumeWork nzResumeWork : works) {
                NZResumeWork _work = nzService.getwork(nzResumeWork.getId());
                _work.setWorkremark(nzResumeWork.getWorkremark());
                editList.add(_work);
            }
            utilService.editList(editList);
            utilService.printJsonResult(true, "操作成功", "no", response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]

    //[start] 招聘信息
    @RequestMapping("/")
    public String Index() {
        return "NZ/index";
    }

    //招聘职位列表
    @RequestMapping(value = "/getjobs", method = RequestMethod.GET)
    public void getjobs(HttpServletRequest request, HttpServletResponse response) {
        try {
            String key = request.getParameter("key").trim();
            StringBuilder str = new StringBuilder();
            List<JobList> jobs = nzService.selectJobs(key);
            int i = 1;
            for (JobList job : jobs) {
                str.append("<tr><td>" + i + "</td><td class='link'><a href='/NZ/job?id=" + job.getId() + "'>" + job.getJobname() + "</a></td>");
                str.append("<td>" + job.getDeptname() + "</td>");
                str.append("<td>" + DateTools.toShortStr(job.getStartDate()) + "</td>");
                str.append("<td>" + DateTools.toShortStr(job.getEndDate()) + "</td></tr>");
                i++;
            }
            if (i == 1) {
                utilService.printTextResult("<tr><td colspan='5'>暂无内部招聘职位</td></tr>", response);
            } else {
                utilService.printTextResult(str.toString(), response);
            }

        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='5'>暂无内部招聘职位</td></tr>", response);
        }
    }

    @RequestMapping("/job")
    public String Job(HttpServletRequest request, Model model) {
        try {
            String id = request.getParameter("id").trim();
            ProjectInfo project = nzService.getProjectByJid(id);
            if (project == null) {
                model.addAttribute("jobinfo", "<tr><td>非法访问！</td></tr>");
            } else {
                if (project.getEnddate().before(new Date())) {
                    model.addAttribute("jobinfo", "<tr><td>职位申请已结束！</td></tr>");
                } else {
					/*PlanPersondemand demand = nzService.getDemand(id);
					StringBuilder str = new StringBuilder();
					str.append("<tr><td width='80'>职位名称：</td><td width='200'>"+demand.getJobname()+"</td>");
					str.append("<td width='80'>职位类别：</td><td>"+EnumTools.JobType.values()[demand.getType()]+"</td></tr>");
					PlanPersondemandnz nz = demandService.getPersondemandnz(demand.getId());
					str.append("<tr><td>招聘方式：</td><td>内部招聘</td><td>入司年限：</td><td>"+nz.getWorkyear()+"年以上</td></tr>");
					str.append("<tr><td>性别要求：</td><td>"+nz.getSex()+"</td>");
					str.append("<td>年龄要求：</td><td>"+nz.getAgemax()+"岁以下</td></tr>");
					str.append("<tr><td>学历要求：</td><td>"+EnumTools.Education.values()[nz.getEducation()]+"</td>");
					str.append("<td>专业要求：</td><td>"+nz.getMajor()+"</td></tr>");
					str.append("<tr><td>职务要求：</td><td>"+nz.getPosition()+"</td><td>职称要求：</td><td>"+nz.getTitle()+"</td></tr>");
					str.append("<tr><td>政治面貌：</td><td colspan='3'>"+nz.getPolitial()+"</td></tr>");
					str.append("<tr><td>工作职责：</td><td colspan='3'>"+demand.getWorkcont()+"</td></tr>");
					str.append("<tr><td>职位要求：</td><td colspan='3'>"+demand.getWorkreq()+"</td></tr>");
					str.append("<tr><td>其他要求：</td><td colspan='3'>"+(nz.getRemark()==null?"无":nz.getRemark())+"</td></tr>");
					model.addAttribute("jobinfo", str);
					
					NZResume resume = utilService.getNZUser(request);
					StringBuilder btns = new StringBuilder();
					if (resume == null) {
						btns.append("<button type='button' class='sq orange' onfocus='blur()' onclick='javascript:location=\"/NZ/login\"';>请先登录</button>");
					} else {
						btns.append("<form id='Validform' method='post' action='/NZ/SendAction'>");
						btns.append("<input type='hidden' name='id' value='" + id + "' />");
						btns.append("<input type='hidden' name='jid' value='" + demand.getId() + "' />");
						btns.append("<input type='hidden' name='jname' value='" + demand.getJobname() + "' />");
						btns.append("<button type='submit' class='sq orange' onfocus='blur()'>立即申请职位</button></form>");
					}
					model.addAttribute("btns", btns);*/
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "NZ/job";
    }

    //申请职位
    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequestMapping(value = "/SendAction", method = RequestMethod.POST)
    public void SendAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String id = request.getParameter("id").trim();//ProjectInfoJob 的ID
            String jid = request.getParameter("jid").trim();//PlanPersondemand 的ID
            String jname = request.getParameter("jname").trim();
            ProjectInfo project = nzService.getProjectByJid(id);
            if (project == null) {
                utilService.printJsonResult(false, "非法访问！", "no", response);
            } else {
                if (project.getEnddate().before(new Date())) {
                    utilService.printJsonResult(false, "职位申请已结束！", "no", response);
                } else {
                    NZResume nzuser = utilService.getNZUser(request);
                    DNZResume dresume = nzService.getDResume(nzuser.getCode(), project.getId());
                    if (dresume != null) {
                        utilService.printJsonResult(false, "一次内部招聘只能申请一个职位！", "no", response);
                    } else {
                        List addList = new ArrayList();
                        NZResume resume = nzService.getResume(nzuser.getCode());
                        DNZResume dresume2 = new DNZResume();
                        BeanTools.Copy(resume, dresume2);

                        ProjectInfoFlow flow1 = nzService.getFlow(project.getId(), 1);
                        ProjectInfoFlow flow2 = nzService.getFlow(project.getId(), 2);
                        String rid = UUID.randomUUID().toString();
                        dresume2.setId(rid);
                        dresume2.setAddDate(new Date());
                        dresume2.setPlanid(project.getPlanid());
                        dresume2.setPlanname(project.getPlanname());
                        dresume2.setPid(project.getId());
                        dresume2.setPname(project.getPname());
                        dresume2.setPath("/" + flow1.getId() + "/" + flow2.getId() + "/");
                        dresume2.setJid(jid);
                        dresume2.setJname(jname);
                        dresume2.setFid(flow2.getId());
                        dresume2.setState("未处理");
                        dresume2.setBdmsg("未发通知");
                        dresume2.setBdstate("未报到");
                        dresume2.setFqstate("未放弃");

                        addList.add(dresume2);

                        flow2.setState(EnumTools.PlanFlowState.进行中.ordinal());

                        //教育背景
                        List<NZResumeEducation> educations = nzService.getEducations(nzuser.getCode());
                        for (NZResumeEducation education : educations) {
                            DNZResumeEducation dEducation = new DNZResumeEducation();
                            BeanTools.Copy(education, dEducation);
                            dEducation.setId(UUID.randomUUID().toString());
                            dEducation.setRid(rid);
                            addList.add(dEducation);
                        }

                        //工作履历
                        List<NZResumeWork> works = nzService.getWorks(nzuser.getCode());
                        for (NZResumeWork work : works) {
                            DNZResumeWork dwork = new DNZResumeWork();
                            BeanTools.Copy(work, dwork);
                            dwork.setId(UUID.randomUUID().toString());
                            dwork.setRid(rid);
                            addList.add(dwork);
                        }

                        utilService.editObject(flow2, addList);
                        utilService.printJsonResult(true, "申请职位成功！", "/NZ/myjob", response);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "申请职位失败！", "no", response);
        }
    }

    //[end]

    //[start] 我的应聘

    @RequestMapping("/myjob")
    public String myjob(HttpServletRequest request, Model model) {
        try {
            String code = utilService.getNZUser(request).getCode();
            List<DNZResume> resumes = nzService.getUsers(code);

            StringBuilder pros = new StringBuilder();
            int i = 0;
            for (DNZResume resume : resumes) {
                StringBuilder str = new StringBuilder();
                //[start] 内部招聘
                List<ProjectInfoFlow> flows = nzService.getProjectInfoFlows(resume.getPid());
                for (ProjectInfoFlow flow : flows) {
                    /*if (flow.getFid() == EnumTools.NZFlowState.职位申请.ordinal()) {
                        str.append("<li>" + DateTools.toDayStr(resume.getAddDate()) + "：完成简历投递</li>");
                    } else */if (flow.getFid() == EnumTools.NZFlowState.报名筛选.ordinal()) {
                        if (resume.getState().equals("未处理")) {
                            str.append("<li><span class='hotitem'>" + "简历筛选中</span></li>");
                        } else if (resume.getState().equals("通过")) {
                            str.append("<li>" + DateTools.toDayStr(resume.getEditDate()) + "：简历筛选通过</li>");
                        } else {
                            str.append("<li><span class='hotitem'>" + DateTools.toDayStr(resume.getEditDate()) + "：简历筛选未通过</span></li>");
                            break;
                        }
                    } else if (flow.getFid() == EnumTools.NZFlowState.面试.ordinal()) {
                        DoMSUser msUser = nzService.getMsUser(flow.getId(), resume.getId());
                        if (msUser == null) {
                            str.append("<li><span class='hotitem'>面试安排中</span></li>");
                        } else {
                            if (msUser.getResult().equals("结果未生成")) {
                                if (msUser.getNotice() == EnumTools.NoticeState.已发通知.ordinal()) {
                                    str.append("<li><span class='hotitem'>面试时间：" + msUser.getStarttime());
                                    str.append("<br>面试地点：" + msUser.getAddress());
                                    str.append("</span></li>");
                                } else {
                                    str.append("<li><span class='hotitem'>面试安排中</span></li>");
                                }
                            } else if (msUser.getResult().equals("通过")) {
                                str.append("<li>" + DateTools.toDayStr(msUser.getPointdate()) + "：面试通过</li>");
                            } else {
                                str.append("<li><span class='hotitem'>" + DateTools.toDayStr(msUser.getPointdate()) + "：面试未通过</span></li>");
                                break;
                            }
                        }
                    } else if (flow.getFid() == EnumTools.NZFlowState.笔试.ordinal()) {
                        DoBSUser bsUser = nzService.getBsUser(flow.getId(), resume.getId());
                        if (bsUser == null) {
                            str.append("<li><span class='hotitem'>笔试安排中</span></li>");
                        } else {
                            if (bsUser.getResult().equals("-")) {
                                if (bsUser.getNotice() == EnumTools.NoticeState.已发通知.ordinal()) {
                                    str.append("<li><span class='hotitem'>笔试时间：" + bsUser.getStarttime());
                                    str.append("<br>笔试地点：" + bsUser.getAddress());
                                    str.append("<br>准考证号：" + bsUser.getTicketnum());
                                    str.append("</span></li>");
                                } else {
                                    str.append("<li><span class='hotitem'>笔试安排中</span></li>");
                                }
                            } else if (bsUser.getResult().equals("通过")) {
                                str.append("<li>" + DateTools.toDayStr(bsUser.getPointdate()) + "：笔试通过</li>");
                            } else {
                                str.append("<li><span class='hotitem'>" + DateTools.toDayStr(bsUser.getPointdate()) + "：笔试未通过</span></li>");
                                break;
                            }
                        }
                    } else if (flow.getFid() == EnumTools.NZFlowState.录用.ordinal()) {
                        if (resume.getBdmsg().equals("未发通知")) {
                            str.append("<li><span class='hotitem'>报到安排中</span></li>");
                        } else {
                            if (resume.getBdstate().equals("未报到")) {
                                str.append("<li><span class='hotitem'>报到时间：" + resume.getBdtime());
                                str.append("<br>报到地点：" + resume.getBdaddress());
                                str.append("<br>报到联系人：" + resume.getBdlxr() + " " + resume.getBdlxdh());
                                str.append("</span></li>");
                            } else {
                                str.append("<li><span class='hotitem'>" + DateTools.toDayStr(resume.getBddate()) + "：已报到</span></li>");
                                break;
                            }
                        }
                    }

                    if (flow.getId().equals(resume.getFid())) {
                        if (resume.getBdstate().equals("未报到")
                                && resume.getFqstate().equals("未放弃")) {
                            str.append("<li><div class='btnbar' style='padding-left:40px;'>");
                            str.append("<form id='giveupValid' method='post' action='/NZ/GiveUpNZ'>");
                            str.append("<input type='hidden' name='pid' value='" + resume.getPid() + "' />");
                            str.append("<button type='submit' class='orange'>放弃本次应聘</button></form></div></li>");
                        }
                        if (resume.getFqstate().equals("已放弃")) {
                            str.append("<li><span class='hotitem'>" + DateTools.toDayStr(resume.getFqdate()) + "：已放弃");
                            str.append("</span></li>");
                        }
                        break;
                    }
                }

                //[end]

                if (i == 0) {
                    pros.append("<div class='bar'><div class='title active'>【" + resume.getPname() + "】：" + resume.getJname() + "</div>");
                    pros.append("<div class='btn'><button type='button' class='show' onFocus='blur()'></button></div>");
                    pros.append("<div class='clear'></div></div><div class='listcont' style='display: block;'><ul>" + str + "</ul></div>");
                } else {
                    pros.append("<div class='bar'><div class='title'>【" + resume.getPname() + "】：" + resume.getJname() + "</div>");
                    pros.append("<div class='btn'><button type='button' class='hide' onFocus='blur()'></button></div>");
                    pros.append("<div class='clear'></div></div><div class='listcont'><ul>" + str + "</ul></div>");
                }

                i++;
            }
            model.addAttribute("result", pros);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/NZ/myjob";
    }

    //[end]

    //[start] 放弃应聘职位(内招)

    @RequestMapping(value = "/GiveUpNZ", method = RequestMethod.POST)
    public void GiveUpNZ(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();
            //登录用户
            NZResume usr = utilService.getNZUser(request);
            DNZResume resume = nzService.getDResume(usr.getCode(), pid);

            if (resume != null && resume.getFqstate().equals("未放弃")
                    && resume.getBdstate().equals("未报到")) {
                resume.setFqdate(new Date());
                resume.setFqfid(resume.getFid());
                resume.setFqstate("已放弃");

                utilService.editObject(resume);
                utilService.printJsonResult(true, "操作成功", "no", response);
            } else {
                utilService.printJsonResult(false, "非法访问", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败", "no", response);
        }
    }

    //[end]
}
